package garin.parse.io;

import java.io.IOException;

public class StringSource extends AbstractSource {
	private final String str;
	int at = -1;
	private char ch;
	
	public StringSource(final String s) {
		str = s;
	}
	
	public long at() {
		return at;
	}
	
	public char ch() {
		return ch;
	}
	
	public String contents(final long start, final long end) throws IOException {
		int sEnd = (int) end;
		final int len = str.length();
		sEnd = sEnd >= len ? len - 1 : sEnd;
		if (sEnd < start) return null;
		return str.substring((int) start, sEnd);
	}
	
	public boolean go() throws IOException {
		at++;
		final boolean b = at < str.length();
		ch = b ? str.charAt(at) : (char) -1;
		return b;
	}
	
	public boolean go(final long pos) throws IOException {
		at = (int) (pos - 1);
		return go();
	}
}
